"""
使用PyMongo持久化电影数据
"""
import time

import bs4
import requests


def generate_cookies():
    cookies_info = '自己浏览器中获取到的cookie信息'
    cookies = {}
    for item in cookies_info.split(';'):
        key, value = item.strip().split('=', maxsplit=1)
        cookies[key] = value
    return cookies


def fetch_page(url):
    resp = requests.get(
        url=url,
        headers={
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) '
                          'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 '
                          'Safari/537.36',
        },
        cookies=generate_cookies(),
    )
    return resp.text


def get_movie_detail(url):
    html_code = fetch_page(url)
    soup = bs4.BeautifulSoup(html_code, 'lxml')
    div = soup.select_one('#info')
    category_spans = div.select('span[property="v:genre"]')
    category = ''
    for index, span in enumerate(category_spans):
        category += span.text
        if index < len(category_spans) - 1:
            category += ' / '
    spans = div.select('#info > span.pl')
    country = spans[1].next_sibling.strip()
    language = spans[2].next_sibling.strip()
    duration_span = div.select_one('span[property="v:runtime"]')
    duration = duration_span.attrs['content']
    return {
        'category': category, 'country': country,
        'language': language, 'duration': duration
    }


def main():
    for page in range(1, 11):
        html_code = fetch_page(f'https://movie.douban.com/top250?start={(page - 1) * 25}')
        soup = bs4.BeautifulSoup(html_code, 'lxml')
        divs = soup.select('div.info')
        for div in divs:
            anchor = div.select_one('div.hd > a')
            detail_url = anchor.attrs['href']
            movie_dict = get_movie_detail(detail_url)
            movie_dict['title'] = anchor.select_one('span:first-Child').text
            movie_dict['rank'] = div.select_one('div.bd > div.star > span.rating_num').text
            motto_span = div.select_one('div.bd > p.quote > span')
            movie_dict['motto'] = motto_span.text if motto_span else ''

        time.sleep(5)


if __name__ == '__main__':
    main()
